using System;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Security;
using DotNetNuke.Entities.Profile;
using DotNetNuke.Entities.Users;


namespace BAS.AsTweet {

    public partial class View : PortalModuleBase {
        private int itemsCount=-1;
        public string customLocalResource;
        #region Event Handlers

        override protected void OnInit(EventArgs e) {
            base.OnInit(e);
            DotNetNuke.Framework.jQuery.RequestRegistration();
            customLocalResource = "~/DesktopModules/BAS_AsTweet/App_LocalResources/View";
        }
        #endregion
        /// <summary>
        /// Number of tweet messages to display
        /// </summary>
        public int TweetItems {
            get { return Settings.ContainsKey(Constants.KEY_ITEMS_COUNT) ? Int32.Parse(Settings[Constants.KEY_ITEMS_COUNT].ToString()) : 10; }
        }


        /// <summary>
        /// Returns twitter screen, if not found string empty
        /// </summary>
        public string TweetScreen {
            get {
                int tweetUserID;
                string propertyScreen; //property with user tweet screen
                string screenName;
                //get the Active Social user whose profile is displayed
                if (Request.QueryString["asuid"] != null && Int32.TryParse(Request.QueryString["asuid"].ToString(), out tweetUserID))
                    ;
                else if (this.UserId > 0)
                    tweetUserID = this.UserId;
                else
                    return "empty";


                if (Settings.ContainsKey(Constants.KEY_OPTIONAL_SCREEN) && Settings[Constants.KEY_OPTIONAL_SCREEN].ToString() != "")
                    propertyScreen = Settings[Constants.KEY_OPTIONAL_SCREEN].ToString();
                else {
                    propertyScreen = "Twitter"; //default Active Social property with twitter account
                }
                
                UserInfo userInfo = new UserController().GetUser(this.PortalId, tweetUserID);                
                ProfilePropertyDefinition propDef = userInfo.Profile.GetProperty(propertyScreen);


                if (propDef != null && !string.IsNullOrEmpty(propDef.PropertyValue)) {
                    screenName = propDef.PropertyValue;
                    screenName = screenName.Split('/')[screenName.Split('/').Length - 1];
                    return screenName;
                } else
                    return "empty";

            }

        }

    }

}
